package tech.aistar.controller.demand;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import tech.aistar.entity.Category;
import tech.aistar.entity.CategoryDetail;
import tech.aistar.service.demand.impl.CategoryDetailServiceImpl;
import tech.aistar.service.demand.impl.CategoryServiceImpl;
import tech.aistar.service.demand.impl.DemandQueryServiceImpl;
import tech.aistar.vo.demand.DemandQueryVo;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;


/**
 * Created by 豆梦寒 on 2019/10/3.
 */
@Controller
@RequestMapping("/demand")
public class DemandListController {

    @Autowired
    private CategoryServiceImpl categoryService;

    @Autowired
    private CategoryDetailServiceImpl categoryDetailService;
//
    @Autowired
    private DemandQueryServiceImpl demandQueryService;
//
//    //    服务列表
    //默认
    @GetMapping("/demandList")
    public String serviceListView(HttpServletRequest req ,Model model )throws ServletException, IOException{
        List<DemandQueryVo> demandAndNameQueryVoList = demandQueryService.findAndName();
        req.setAttribute("demandAndNameQueryVoList",demandAndNameQueryVoList);
        return demandCategory(model);
    }


    /**
     * 根据需求名称模糊查询
     */
    @RequestMapping("/selectAll")
    public String selectAll(HttpServletRequest req,Model model)throws ServletException, IOException {
        //接受查询的参数
        String title = req.getParameter("title");//根据名称模糊查询
        //数据的回显
        req.setAttribute("title",title==null?"":title);

        List<DemandQueryVo> demandAndNameQueryVoList = demandQueryService.findByName(title);

        req.setAttribute("demandAndNameQueryVoList",demandAndNameQueryVoList);

        return demandCategory(model);
    }
    /**
     * 根据类别查询
     */
    @GetMapping("/selectByCategory")
    public String selectByCategory(HttpServletRequest req ,Model model )throws ServletException, IOException{
        Integer categoryId = Integer.valueOf(req.getParameter("category_id"));
        System.out.println(categoryId);
        List<DemandQueryVo> demandAndNameQueryVoList = demandQueryService.findByCategory(categoryId);
        req.setAttribute("demandAndNameQueryVoList",demandAndNameQueryVoList);
        return demandCategory(model);
    }

    /**
     * 根据详细类别查询
     */
    @GetMapping("/selectByCategoryDetail")
    public String selectByCategoryDetail(HttpServletRequest req ,Model model )throws ServletException, IOException{
        Integer categoryDetailId = Integer.valueOf(req.getParameter("categorydetail_id"));
        System.out.println(categoryDetailId);
        List<DemandQueryVo> demandAndNameQueryVoList = demandQueryService.findByCategoryDetail(categoryDetailId);
        req.setAttribute("demandAndNameQueryVoList",demandAndNameQueryVoList);
        return demandCategory(model);
    }

    /**
     * 根据发布时间查询
     */
    @GetMapping("/demandBybeginTime")
    public String servicesBybeginTime(HttpServletRequest req ,Model model )throws ServletException, IOException{
        List<DemandQueryVo> demandAndNameQueryVoList = demandQueryService.findBybeginTime();
        req.setAttribute("demandAndNameQueryVoList",demandAndNameQueryVoList);
        return demandCategory(model);
    }

    /**
     * 根据成交数降序查询
     */
    @GetMapping("/demandByCount")
    public String servicesByCount(HttpServletRequest req ,Model model )throws ServletException, IOException{
        List<DemandQueryVo> demandAndNameQueryVoList = demandQueryService.findByCount();
        req.setAttribute("demandAndNameQueryVoList",demandAndNameQueryVoList);
        return demandCategory(model);
    }

    /**
     * 根据价格降序查询
     */
    @GetMapping("/demandByPriceDesc")
    public String servicesByPriceDesc(HttpServletRequest req ,Model model )throws ServletException, IOException{
        List<DemandQueryVo> demandAndNameQueryVoList = demandQueryService.findByPriceDesc();
        req.setAttribute("demandAndNameQueryVoList",demandAndNameQueryVoList);
        return demandCategory(model);
    }

    /**
     * 根据价格升序查询
     */
    @GetMapping("/demandByPrice")
    public String servicesByPrice(HttpServletRequest req ,Model model )throws ServletException, IOException{
        List<DemandQueryVo> demandAndNameQueryVoList = demandQueryService.findByPrice();
        req.setAttribute("demandAndNameQueryVoList",demandAndNameQueryVoList);
        return demandCategory(model);
    }

    /**
     * 根据价格区间查询
     */
    @GetMapping("/demandByPriceRange")
    public String servicesByPriceRange(HttpServletRequest req ,Model model )throws ServletException, IOException{
        String minPrice = req.getParameter("minPrice");
        Double minPrice1 = Double.valueOf(minPrice);
        System.out.println("minPrice1 = " + minPrice1);
        String maxPrice = req.getParameter("maxPrice");
        Double maxPrice1 = Double.valueOf(maxPrice);
        System.out.println("maxPrice1 = " + maxPrice1);
        List<DemandQueryVo> demandAndNameQueryVoList = demandQueryService.findByPriceRange(minPrice1,maxPrice1);
        req.setAttribute("demandAndNameQueryVoList",demandAndNameQueryVoList);
        return demandCategory(model);
    }



    //    类别
    @GetMapping("/demandCategory")
    public String demandCategory(Model model ){
        List<Category> categoryList =  categoryService.findAll();
        List<CategoryDetail> categoryDetailList = categoryDetailService.findAl();
        model.addAttribute("categoryList",categoryList);
        model.addAttribute("categoryDetailList",categoryDetailList);
        return "demand/demandList";
    }

}
